home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- // sound spheres, a program demonstrating Inventor 2.0 and the Audio Library
- // written by Kevin Goldsmith at SGI - spring '94
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <math.h>
- #include <getopt.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <Inventor/Xt/SoXt.h>
- #include <Inventor/Xt/viewers/SoXtPlaneViewer.h>
- #include <Inventor/nodes/SoSphere.h>
- #include <Inventor/nodes/SoSeparator.h>
- #include <Inventor/nodes/SoTransform.h>
- #include <Inventor/nodes/SoMaterial.h>
- #include <Inventor/sensors/SoFieldSensor.h>
- #include <Inventor/draggers/SoDragPointDragger.h>
- #include <Inventor/engines/SoCalculator.h>
-
- #include "PlayClass.h"
-
- /*
- * local defines
- */
-
- #ifndef FALSE
- #define FALSE (0)
- #define TRUE (!FALSE)
- #endif
- #ifndef ABS
- #define ABS(a) ((a)>0.0?(a):-(a))
- #endif
- #ifndef MIN
- #define MIN(a, b) ((a)<(b)?(a):(b))
- #endif
- #ifndef MAX
- #define MAX(a, b) ((a)>(b)?(a):(b))
- #endif
- #define RANGE(a, b1, b2) \
- \
- ((b1)<(b2)? \
- ((a)<(b1)? \
- (b1): \
- ((a)>(b2)? \
- (b2):(a))): \
- ((a)<(b2)? \
- (b2): \
- ((a)>(b1)? \
- (b1):(a))))
-
- #ifndef INTERP
- #define INTERP(x1, x2, a) ((a)*(x2)+(1.0-(a))*(x1))
- #endif
-
- double xpos, ypos, ipos, jpos;
- char *filename[4];
- int num = 0;
-
- typedef struct {
- PlayClass *pc;
- SoTransform *trans;
- } CBstruct;
-
- typedef struct {
- PlayClass *pc[4];
- int num;
- SoTransform *trans;
- } CBstruct2;
-
-
- static void
- emitCB(void *userData, SoSensor *)
- {
-
- CBstruct *cbs = (CBstruct *) userData;
- cbs->pc->setEmitterPosition((cbs->trans->translation.getValue()[0]),
- (cbs->trans->translation.getValue()[1]),
- (cbs->trans->translation.getValue()[2]));
- }
-
- static void
- recCB(void *userData, SoSensor *)
- {
-
- CBstruct2 *cbs = (CBstruct2 *) userData;
- for (int i = 0; i < cbs->num; i++) {
- cbs->pc[i]->setReceiverPosition((cbs->trans->translation.getValue()[0]),
- (cbs->trans->translation.getValue()[1]),
- (cbs->trans->translation.getValue()[2]));
- }
- }
-
- static void
- parseCommandLine(int argc, char **argv)
- {
- int err = 0; // Flag: error in options?
- int c;
-
- while (( c = getopt(argc, argv, "")) != -1)
- {
- switch(c)
- {
- default:
- err = 1;
- break;
- }
-
- }
-
-
- if (optind == argc) err = 1;
-
- else {
- if (argc > optind )
- filename[num++] = argv[optind];
- if (argc > optind + 1)
- filename[num++] = argv[optind + 1];
- if (argc > optind + 2)
- filename[num++] = argv[optind + 2];
- if (argc > optind + 3)
- filename[num++] = argv[optind + 3];
- }
-
- if (err)
- {
- fprintf(stderr,
- "Usage: %s filename1 [filename2] [filename3] [filename4]\n",
- argv[0]);
- exit(99);
- }
- }
-
- main(int argc, char **argv)
- {
- PlayClass *pc[4];
- int i;
-
- ipos = jpos = xpos = ypos = 0.0;
- for (i = 0; i < 4; i++) filename[i] = NULL;
-
- parseCommandLine(argc,argv);
-
- for (i = 0; i < num; i++) {
- pc[i] = new PlayClass(argv[0]);
-
- pc[i]->setFilename(filename[i]);
- pc[i]->setUseDistance(TRUE);
- pc[i]->setLoop(TRUE);
- }
-
- // Initialize Inventor. This will return a main window to use.
- // If unsuccessful, exit.
- Widget appWindow = SoXt::init(argv[0]); // pass the app name
- if ( appWindow == NULL ) exit( 1 );
-
- // create the top level separators
- SoSeparator *root = new SoSeparator;
- root->ref();
-
- SoSeparator *draggerSep = new SoSeparator;
- root->addChild(draggerSep);
-
- // create the transform that will make the draggers larger
- SoTransform *draggerTrans = new SoTransform;
- draggerTrans->scaleFactor.setValue(2.0, 2.0, 2.0);
- draggerSep->addChild(draggerTrans);
-
- // create the nodes needed for the emitters
- SoSeparator *emitSep[4];
- SoTransform *emitTrans[4];
- SoMaterial *emitMat[4];
- SoSphere *emitSph[4];
- SoDragPointDragger *emitDragger[4];
- SoCalculator *emitCalc[4];
-
- // create and initalize the emitters
- for (i = 0; i < num; i++) {
- emitSep[i] = new SoSeparator;
- emitTrans[i] = new SoTransform;
- emitMat[i] = new SoMaterial;
- emitDragger[i] = new SoDragPointDragger;
- emitCalc[i] = new SoCalculator;
- emitCalc[i]->ref();
-
- // initialize the location and color of the emitter
- switch (i) {
- case 0:
- emitDragger[i]->translation.setValue( - 5.0, 0.0, - 5.0);
- emitMat[i]->diffuseColor.setValue(1.0,0.0,0.0);
- break;
- case 1:
- emitDragger[i]->translation.setValue(5.0, 0.0, 5.0);
- emitMat[i]->diffuseColor.setValue(0.0,1.0,0.0);
- break;
- case 2:
- emitDragger[i]->translation.setValue(- 5.0, 0.0, 5.0);
- emitMat[i]->diffuseColor.setValue(0.0,0.0,1.0);
- break;
- case 3:
- emitDragger[i]->translation.setValue(5.0, 0.0, - 5.0);
- emitMat[i]->diffuseColor.setValue(1.0,0.0,1.0);
- break;
- default:
- emitDragger[i]->translation.setValue(0.0, 0.0, 0.0);
- emitMat[i]->diffuseColor.setValue(1.0,1.0,1.0);
- break;
- }
-
- // add the new nodes to the scene
- emitSep[i]->addChild(emitTrans[i]);
- emitSep[i]->addChild(emitMat[i]);
- emitSph[i] = new SoSphere;
- emitSep[i]->addChild(emitSph[i]);
- root->addChild(emitSep[i]);
- draggerSep->addChild(emitDragger[i]);
-
- // create a calculator to counteract the scale affecting the draggers
- emitCalc[i]->A.connectFrom(&emitDragger[i]->translation);
- emitCalc[i]->B.connectFrom(&draggerTrans->scaleFactor);
- emitCalc[i]->expression= "oA = vec3f(A[0]*B[0], A[1]*B[1], A[2]*B[2])";
-
- // connect the translation of the emitter to the output of the calc
- emitTrans[i]->translation.connectFrom(&emitCalc[i]->oA);
-
- CBstruct *cbs = new CBstruct;
- cbs->pc = pc[i];
- cbs->trans = emitTrans[i];
-
- // create a sensor that will notify us when the translation changes
- // so that we can update the audio
- SoFieldSensor *emitSens = new SoFieldSensor(emitCB, cbs);
- emitSens->attach(&emitTrans[i]->translation);
- }
-
-
- // create the receiver nodes
- SoSeparator *recSep = new SoSeparator;
- SoTransform *recTrans = new SoTransform;
- recSep->addChild(recTrans);
- SoMaterial *recMat = new SoMaterial;
- recSep->addChild(recMat);
- SoSphere *recSph = new SoSphere;
- recSep->addChild(recSph);
- root->addChild(recSep);
-
- // create the dragger and calculator used by the receiver
- SoDragPointDragger *recDragger = new SoDragPointDragger();
- SoCalculator *recCalc = new SoCalculator;
- recCalc->ref();
- recCalc->A.connectFrom(&recDragger->translation);
- recCalc->B.connectFrom(&draggerTrans->scaleFactor);
- recCalc->expression = "oA = vec3f(A[0]*B[0], A[1]*B[1], A[2]*B[2])";
-
- recTrans->translation.connectFrom(&recCalc->oA);
- draggerSep->addChild(recDragger);
-
- CBstruct2 *cbs3 = new CBstruct2;
- cbs3->num = num;
- for (i = 0; i < num; i++) cbs3->pc[i] = pc[i];
- cbs3->trans = recTrans;
-
- SoFieldSensor *recSens = new SoFieldSensor(recCB, cbs3);
- recSens->attach(&recTrans->translation);
-
-
- // set up the play classes so that they have the correct locations of
- // the emitter and receiver
- for (i = 0; i < num; i++) {
- pc[i]->setEmitterPosition((emitTrans[i]->translation.getValue())[0],
- (emitTrans[i]->translation.getValue())[1],
- (emitTrans[i]->translation.getValue())[2]);
- pc[i]->setReceiverPosition((recTrans->translation.getValue())[0],
- (recTrans->translation.getValue())[1],
- (recTrans->translation.getValue())[2]);
- pc[i]->start(); // begin the audio
- }
-
- SoXtPlaneViewer *viewer = new SoXtPlaneViewer(appWindow);
- viewer->setSceneGraph(root);
- viewer->show();
-
- SoXt::show(appWindow);
- SoXt::mainLoop();
-
- while(TRUE) {}
- }
-